<template>
	<cl-crud ref="Crud">
		<cl-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<!-- <cl-add-btn /> -->
			<!-- 删除按钮 -->
			<!-- <cl-multi-delete-btn /> -->
			<cl-flex1 />
			<!-- 条件搜索 -->
			<cl-search ref="Search" />
		</cl-row>

		<cl-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</cl-row>

		<cl-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</cl-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" setup>
defineOptions({
	name: 'order-order'
});

import { useCrud, useTable, useUpsert, useSearch } from '@cool-vue/crud';
import { useCool } from '/@/cool';
import { useI18n } from 'vue-i18n';
import { reactive } from 'vue';

const { service } = useCool();
const { t } = useI18n();

// 选项
const options = reactive({
	payStatus: [
		{ label: t('待支付'), value: 0 },
		{ label: t('已支付'), value: 1 },
		{ label: t('已关闭'), value: 2 }
	]
});

// cl-upsert
const Upsert = useUpsert({
	items: [
		{
			label: t('订单编号'),
			prop: 'orderNo',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12,
			required: true
		},
		{
			label: t('支付金额'),
			prop: 'payAmount',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12,
			required: true
		},
		{
			label: t('支付状态'),
			prop: 'payStatus',
			component: { name: 'el-radio-group', options: options.payStatus },
			value: 0,
			required: true
		},
		{
			label: t('支付时间'),
			prop: 'payTime',
			component: {
				name: 'el-date-picker',
				props: { type: 'datetime', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
			},
			span: 12
		},
		{
			label: t('选择支付用户'),
			prop: 'payUserId',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12
		},
		{
			label: t('设备号'),
			prop: 'deviceNo',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12,
			required: true
		},
		{
			label: t('选择设备归属人'),
			prop: 'deviceBindUserId',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12
		},
		{
			label: t('退款原因'),
			prop: 'refundReason',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12
		},
		{
			label: t('退款时间'),
			prop: 'refundTime',
			component: {
				name: 'el-date-picker',
				props: { type: 'datetime', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
			},
			span: 12
		},
		{
			label: t('是否完成投币'),
			prop: 'isComplete',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12,
			required: true
		},
		{
			label: t('套餐内容'),
			prop: 'packageContent',
			component: { name: 'el-input', props: { clearable: true } },
			span: 12
		}
	]
});

// cl-table
const Table = useTable({
	columns: [
		{ type: 'selection' },
		{ label: t('订单编号'), prop: 'orderNo', minWidth: 120 },
		{ label: t('设备归属'), prop: 'deviceBindNickName', minWidth: 120 },
		{ label: t('归属人手机号'), prop: 'deviceBindPhone', minWidth: 120 },
		{
			label: t('支付金额'),
			prop: 'payAmount',
			minWidth: 120,
			formatter: row => `￥${row.payAmount / 100}`
		},
		{
			label: t('支付状态'),
			prop: 'payStatus',
			minWidth: 120,
			dict: options.payStatus
		},
		{ label: t('支付用户ID3'), prop: 'payUserId', minWidth: 120 },
		{ label: t('设备号'), prop: 'deviceNo', minWidth: 120 },
		{
			label: t('套餐内容'),
			prop: 'packageContent',
			minWidth: 120,
			render: _row => {
				const _packinfo = _row.packageContent ? JSON.parse(_row.packageContent) : undefined;

				return _packinfo
					? `${_packinfo?.packagePrice / 100}元 = ${_packinfo?.gameCount}币`
					: '无';
			}
		},
		{
			label: t('创建时间'),
			prop: 'createTime',
			minWidth: 170,
			sortable: 'desc',
			component: { name: 'cl-date-text' }
		}
	]
});

// cl-search
const Search = useSearch();

// cl-crud
const Crud = useCrud(
	{
		service: service.order.order
	},
	app => {
		app.refresh();
	}
);

// 刷新
function refresh(params?: any) {
	Crud.value?.refresh(params);
}
</script>
